<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui-flex.css" />
    <style>
    </style>
</head>
<style>
    .home:before {
        content: url(../image/home.png);
    }

    .refresh:before {
        content: url(../image/refresh.png);
    }

    .company:before {
        content: url(../image/company.png);
    }

    .pa-title {
        font-size: 17px;
        color: black;
        font-weight: bold;
    }

    .pa-title-content {
        font-size: 17px;
        color: black;
    }

    body {
        background-color: #D9D8DB;
    }

    .pa_scbg_ng {
        background-color: #254060;
        border-radius: 5px;
        height: 442px;
        color: white;
    }

    .pa_scbg_left_top {
        height: 70px;
        margin-top: 10px;
    }

    .pa_scbg_left_center {
        height: 170px;
    }

    .pa_scbg_left_bottom {
        height: 185px;
        margin-top: 5px;
        overflow: auto;
    }

    .pa_scbg_ng_but {
        height: 105px;
        line-height: 105px;
        margin-left: 30px;
        margin-top: 35px;
        float: left;
        width: 105px;
        font-size: 30px;
        color: white;
        text-align: center;
    }

    .pa_scbg_ng_c {
        margin-top: 10px;
        height: 30px;
        line-height: 30px;
        width: 95%;
        margin-left: 3%;
        text-align: center;
    }

    .pa_scbg_ng_c input {
        height: 30px;
        min-height: 30px;
    }

    .pa_scbg_ng_c select {
        height: 30px;
        min-height: 30px;
    }

    p {
        font-size: 17px;
        color: black;
        font-weight: bold;
        text-align: center;
    }

    table {
        width: 100%;
        table-layout: fixed;
        word-wrap: break-word;
    }

    td {
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
    }
</style>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #D9D8DB">
        <div style="width: 10%;height: 60px;float: left;padding-top: 6px;">
            <a class="aui-pull-left aui-btn ">
                <span class="aui-iconfont company"></span>
            </a>
        </div>
        <div style="width: 10%;height: 60px;float: left;">
            <div style="text-align: left;">
                <span class="pa-title">富晟安创</span>
            </div>
        </div>
        <div style="width: 70%;height: 60px;float: left;">
            <div style="width:18%; float: left; text-align: left;">
                <span class="pa-title-content" id="locationName"></span>
            </div>
            <div style="width:18%; float: left; text-align: left;">
                <span class="pa-title-content" id="workCenterName"></span>
            </div>
            <div style="width:24%; float: left; text-align: left;">
                <span class="pa-title-content" id="workCellName"></span>
            </div>
            <div style="width:40%; float: left; text-align: left;">
                <span class="pa-title-content" id="nowDate"></span>
            </div>
        </div>
        <div style="width: 5%;height: 50px;float: left;">
            <a class="aui-pull-right aui-btn " onclick="refresh()">
                <span class="aui-iconfont refresh"></span>
            </a>
        </div>
        <div style="width: 5%;height: 50px;float: left;">
            <a class="aui-pull-right aui-btn" onclick="closeWin()">
                <span class="aui-iconfont home"></span>
            </a>
        </div>
    </header>
    <div class="aui-content">
        <div class="aui-flex-col aui-flex-center" style="height: 30px;">
            <div class="aui-flex-item-12">
                <p>不合格品报工</p>
            </div>
        </div>

        <div class="aui-flex-col aui-flex-center pa_scbg_ng" style="margin-left: 1%;margin-right: 1%;">
            <div class="aui-flex-item-12">
                <div class="aui-flex-col pa_scbg_left_top">
                    <div style="height: 100%;width: 98%;margin-left: 1%;">
                        <table class="table" border="1px solid gray" cellspacing="0" cellpadding="0">
                            <thead>
                                <tr style="text-align: center;color: white;font-size: 18px;height: 30px;">
                                    <td algin='center' style="width:18%">工单号</td>
                                    <td algin='center' style="width:18%">物料编码</td>
                                    <td algin='center' style="width:19%">物料名称</td>
                                    <td algin='center' style="width:15%">可报工数量</td>
                                    <td algin='center' style="width:15%">已报工数量</td>
                                    <td algin='center' style="width:15%">批次号</td>
                                </tr>
                            </thead>
                            <tbody id="workOrderDataHtml">
                            </tbody>
                        </table>
                    </div>
                </div>

                <div class="aui-flex-col pa_scbg_left_center">
                    <div class="aui-flex-item-8">
                        <div class="aui-flex-col pa_scbg_ng_c">
                            <div class="aui-list-item-inner" style="border: 0;">
                                <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                                    容器条码：</div>
                                <div class="aui-list-item-input"
                                    style="background-color: white;float: left;width: 400px;">
                                    <input type="text" id="containerTm" class="aui-input" onclick="getIdSelector();" />
                                </div>
                            </div>
                        </div>

                        <div class="aui-flex-col pa_scbg_ng_c">
                            <div class="aui-list-item-inner" style="border: 0;">
                                <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                                    缺陷类别：</div>
                                <div class="aui-list-item-input" style="background-color: white;float: left;">
                                    <select id="chkItemType" style="width: 400px;" onchange="onChange()">
                                    </select>
                                </div>
                            </div>
                        </div>

                        <div class="aui-flex-col pa_scbg_ng_c">
                            <div class="aui-list-item-inner" style="border: 0;">
                                <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                                    缺陷名称：</div>
                                <div class="aui-list-item-input" style="background-color: white;float: left;">
                                    <select id="chkItem" style="width: 400px;">
                                        <option value=''>--请选择--</option>
                                    </select>
                                </div>
                            </div>
                        </div>

                        <div class="aui-flex-col pa_scbg_ng_c">
                            <div class="aui-list-item-inner" style="border: 0;">
                                <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                                    产品条码：</div>
                                <div class="aui-list-item-input"
                                    style="background-color: white;float: left;width: 400px;">
                                    <input type="text" id="produBarcode" class="aui-input" onclick="getIdSelector();" />
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="aui-flex-item-4">
                        <div class="aui-flex-col">
                            <div class="pa_scbg_ng_but" style="background-color: #D9001B;" onclick="back()">撤销</div>
                            <div class="pa_scbg_ng_but" style="background-color: #D9001B;" onclick="cancle()">关闭</div>
                        </div>
                    </div>
                </div>

                <div class="aui-flex-col pa_scbg_left_bottom">
                    <div class="aui-flex-item-12">
                        <div style="height: 100%;width: 100%;width: 98%;margin-left: 1%;">
                            <table class="table" border="1px solid gray" cellspacing="0" cellpadding="0">
                                <thead>
                                    <tr style="text-align: center;color: white;font-size: 18px;height: 30px;">
                                        <td algin='center' style="width:5%;"></td>
                                        <td algin='center' style="width:7%">序号</td>
                                        <td algin='center' style="width:20%">容器码</td>
                                        <td algin='center' style="width:33%">产品条码</td>
                                        <td algin='center' style="width:15%">批次号</td>
                                        <td algin='center' style="width:20%">报工时间</td>
                                        <td style="display: none;">sysid</td>
                                    </tr>
                                </thead>
                                <tbody id="reportDataHtml">
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">

    var equipUniqueId = "";
    //光标所在id
    var idSelector = "";

    //初始化
    apiready = function () {
        //获取设备唯一号
        equipUniqueId = localStorage.getItem('equipUniqueId');
        //产线工位信息
        document.getElementById("locationName").innerHTML = api.pageParam.locationName;
        document.getElementById("workCenterName").innerHTML = api.pageParam.workCenterName;
        document.getElementById("workCellName").innerHTML = api.pageParam.workCellName;
        document.getElementById("nowDate").innerHTML = getTimeCommon();
        //当前时间刷新
        window.setInterval("getTime()", 1000);

        api.setFocus({
            inputId: 'containerTm',
            focus: true
        })

        //查询工单信息
        doSearchWorkOrder();

        //查询报工信息
        doSearchReport();

        //查询缺陷类别下拉框
        doSearchCombox();
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //时间刷新
    function getTime() {
        document.getElementById("nowDate").innerHTML = getTimeCommon();
    }

    //缺陷类别下拉框
    function doSearchCombox() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getUtilComboxList";
        var data = {
            values: {
                tablename: "qc_bd_chk_item_type",
                column_name: "name",
                column_value: "sysid",
                condition: "is_delete=0",
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == 0) {
                    var list = eval(myobj.list);
                    var html = "";
                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                    }

                    $api.html($api.byId("chkItemType"), html);
                } else {
                    $api.html($api.byId("chkItemType"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }

    //缺陷类别值改变
    function onChange() {
        var chkItemTypeId = $("#chkItemType").val();
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getUtilComboxList";
        var data = {
            values: {
                tablename: "qc_qual_chk_item",
                column_name: "name",
                column_value: "sysid",
                condition: "chk_item_type_gid= '" + chkItemTypeId + "'",
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == 0) {
                    var list = eval(myobj.list);
                    var html = "";
                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                    }

                    $api.html($api.byId("chkItem"), html);
                } else {
                    $api.html($api.byId("chkItem"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }

    //查询工单信息
    function doSearchWorkOrder() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyWorkOrder";
        var data = {
            values: {
                workOrderCode: api.pageParam.workOrderCode,
                proddate: api.pageParam.proddate,
                workCenterCode: api.pageParam.workCenterCode,
                workCenterName: api.pageParam.workCenterName,
                shiftCode: api.pageParam.shiftCode,
                state: "开工",
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == 0) {

                    var list = eval(myobj.list);
                    var html = "";
                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;color: white;font-size: 15px;height:35px;'>";
                        html += "<td><span id='workOrderCode'>" + list[i].workOrderCode + "</span></td>";
                        html += "<td name='mrlCode'>" + list[i].mrlCode + "</td>";
                        html += "<td name='mrlName'>" + list[i].mrlName + "</td>";
                        html += "<td><span id='overageQty'>" + (list[i].planQty - list[i].completeQty) + "</span></td>";
                        html += "<td name='completeQty'>" + list[i].completeQty + "</td>";
                        html += "<td name='lotCode'>" + list[i].lotCode + "</td>";
                        html += "</tr>";
                    }

                    $api.html($api.byId("workOrderDataHtml"), html);
                } else {
                    $api.html($api.byId("workOrderDataHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }

    //查询报工信息
    function doSearchReport() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyReport";
        var data = {
            values: {
                workOrderCode: api.pageParam.workOrderCode,
                type: '1',  //合格报工数据:0-合格，1-不合格
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == 0) {
                    var list = eval(myobj.list);
                    var html = "";
                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;color: white;font-size: 15px;height:30px;'>";
                        html += "<td><input name='sub' type='checkbox' /></td>"
                        html += "<td name='index'>" + (i + 1) + "</td>";
                        html += "<td name='containerTm'>" + list[i].containerTm + "</td>";
                        html += "<td name='produBarcode'>" + list[i].produBarcode + "</td>";
                        html += "<td name='lotCode'>" + list[i].lotCode + "</td>";
                        html += "<td name='endTimestamp'>" + list[i].endTimestamp + "</td>";
                        html += "<td name='sysid' style='display:none;'>" + list[i].sysid + "</td>";
                        html += "</tr>";
                    }

                    $api.html($api.byId("reportDataHtml"), html);
                } else {
                    $api.html($api.byId("reportDataHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }

    //撤销
    function back() {
        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        var sysid = "";
        check.each(function () { //遍历
            var row = $(this).parent("td").parent("tr");
            sysid = row.find("[name='sysid']").html();
            count++;
        });

        if (count != 1) {
            api.toast({
                msg: "请选择一条表格数据",
                duration: 3000
            });
            return;
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/assemblyReportReturnUniqueSave";
        var data = {
            values: {
                sysid: sysid,
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        api.showProgress({
            title: '保存中...',
        });
        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                api.hideProgress();
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == 0) {
                    api.toast({
                        msg: "撤销成功",
                        duration: 3000
                    });

                    //刷新数据
                    refresh();

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                api.hideProgress();
                alert("网络连接超时!")
            }
        });
    }

    //条码回车事件
    $("#containerTm").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                $("#containerTm").blur();
                api.setFocus({
                    inputId: 'produBarcode',
                    focus: true
                });
            }
        }
    });

    //条码回车事件
    $("#produBarcode").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                $("#produBarcode").blur();
                save();
            }
        }
    });

    //保存
    function save() {
        var workOrderCode = $("#workOrderCode").html();
        var overageQty = parseInt($("#overageQty").html());
        var containerTm = $("#containerTm").val();
        var produBarcode = $("#produBarcode").val();
        var chkItemType = $("#chkItemType").val();
        var chkItem = $("#chkItem").val();

        //工单号非空校验
        if (workOrderCode == null || workOrderCode == "" || workOrderCode == undefined) {
            api.toast({
                msg: '工单号不能为空！',
                duration: 3000
            });
            return;
        }

        //容器条码非空校验
        if (containerTm == null || containerTm == "" || containerTm == undefined) {
            api.toast({
                msg: '请扫描/录入容器条码！',
                duration: 3000
            });
            return;
        }

        //缺陷类别非空校验
        if (chkItemType == null || chkItemType == "" || chkItemType == undefined) {
            api.toast({
                msg: '缺陷类别不能为空！',
                duration: 3000
            });
            return;
        }

        //缺陷名称非空校验
        if (chkItem == null || chkItem == "" || chkItem == undefined) {
            api.toast({
                msg: '缺陷名称不能为空！',
                duration: 3000
            });
            return;
        }

        //产品条码非空校验
        if (produBarcode == null || produBarcode == "" || produBarcode == undefined) {
            api.toast({
                msg: '请扫描产品条码！',
                duration: 3000
            });
            return;
        }

        //可报工数
        if (overageQty == 0) {
            api.toast({
                msg: '可报工数量为【0】！',
                duration: 3000
            });
            return;
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyReportUniqueSave";
        var data = {
            values: {
                workOrderCode: workOrderCode,
                workCenterCode: api.pageParam.workCenterCode,
                workCellCode: api.pageParam.workCellCode,
                containerTm: containerTm,
                produBarcode: produBarcode,
                chkItemType: chkItemType,
                chkItem: chkItem,
                type: "1",  //0合格 1不合格 
                scrapFlag: "1",  //是否存报废表标识：电镀下挂时已存过（0不存，1存）
                un: un,
            }
        };

        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        api.showProgress({
            title: '保存中...',
        });

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                api.hideProgress();
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == 0) {

                    //清空条码
                    $("#produBarcode").val("");
                    api.toast({
                        msg: "保存成功!",
                        duration: 3000
                    });

                    //刷新表数据
                    refresh();

                    //光标放产品条码上
                    api.setFocus({
                        inputId: 'produBarcode',
                        focus: true
                    });
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                api.hideProgress();
                alert("网络连接超时!")
            }
        });
    }

    //刷新按钮
    function refresh() {
        //获取设备唯一号
        equipUniqueId = localStorage.getItem('equipUniqueId');
        var workOrderCode = $("#workOrderCode").html();
        if (workOrderCode != null && workOrderCode != "" && workOrderCode != undefined) {
            //查询工单信息
            doSearchWorkOrder();

            //查询报工数据
            doSearchReport();
        }
    }

    //返回上级页面
    function cancle() {
        api.closeWin({});
        api.execScript({
            name: "productionAssemblyScbg_unique",
            script: 'refresh()'
        });
    }

    //返回按钮
    function closeWin() {
        api.closeToWin({ name: api.pageParam.rootName });
        api.execScript({
            name: api.pageParam.rootName,
            script: 'refresh()'
        });
    }
</script>

</html>